<?php
namespace app\admin\controller;

use app\admin\exception\AdminException;
use app\common\entity\GameRoomLog;
use app\common\entity\Goods;
use app\common\entity\Video;
use app\common\shop\ShopGoodsModel;
use think\Db;
use think\Request;
use app\common\entity\Mywallet;
use app\common\entity\GameRoom;
use app\common\entity\GameRoomConfig;

class Game extends Admin
{

    /**
     * 房间列表
     * */
    public function roomList(Request $request){
        $entity = GameRoom::field('*');
        $config = GameRoomConfig::select();
        if($type = $request->param('type')){
            $entity->where('size',$type);
        }

        $list = $entity
            ->paginate(15, false, [
            'query' => $request->param()??[]
        ]);

        return $this->render('room_list',[
            'list'=>$list,
            'cate'=>$config,
            'config'=>new GameRoomConfig(),
        ]);
    }

    /**
     * 房间详情
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function roomDetail(Request $request){
        $config = GameRoomConfig::select();
        if($request->param('id')){
            $info = GameRoom::where('id',$request->param('id'))->find();

            return $this->render('room_detail',[
                'video'=>Video::select(),
                'info'=>$info,
                'cate'=>$config,
                'goodsList' => $list = ShopGoodsModel::alias('g')
                    ->field('g.*,ga.category_title')
                    ->where('g.shoptype',3)
                    ->leftJoin('goods_category ga','ga.category_id = g.cateid')
                    ->order('g.id desc')
                    ->select()
            ]);
        }else{
            return $this->render('room_detail', [
                'video'=>Video::select(),
                'cate'=>$config,
                'goodsList' => Db::name('shop_goods')->where('shoptype',3)->select(),
            ]);
           /* return $this->render('room_detail',[
                'cate'=>$config,
            ]);*/
        }

    }

    /**
     * 添加房间
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function roomAdd(Request $request){

        $config = GameRoomConfig::where('size',$request->post('size'))->find();
        if(!$config){
            return json(['code'=>1,'message'=>'请选择房间大小']);
        }
        $data = [
            'name'=>$request->post('name'),
            'size'=>$request->post('size'),
            'money'=>$request->post('money'),
            'status'=>0,
            'num'=>$request->post('num'),
            'goods_id'=>$request->post('goods_id'),
            'goods_title'=>$request->post('goods_title'),
            'video'=>$request->post('video'),
            'update_time'=>time(),
        ];
        if(GameRoom::insert($data)){
            return json(['code'=>0,'message'=>'操作成功']);
        }
        return json(['code'=>1,'message'=>'操作失败']);
    }

    /**
     * 修改房间
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function roomEdit(Request $request){
        $config = GameRoomConfig::where('size',$request->post('size'))->find();
        if(!$config){
            return json(['code'=>1,'message'=>'请选择房间大小']);
        }
        $data = [
            'name'=>$request->post('name'),
            'size'=>$request->post('size'),
            'money'=>$request->post('money'),
            'status'=>0,
            'num'=>$request->post('num'),
            'goods_id'=>$request->post('goods_id'),
            'goods_title'=>$request->post('goods_title'),
            'video'=>$request->post('video'),
            'update_time'=>time(),
        ];
        if(GameRoom::where('id',$request->param('id'))->update($data)){
            return json(['code'=>0,'message'=>'操作成功']);
        }
        return json(['code'=>1,'message'=>'操作失败']);
    }

    /**
     * 删除房间
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function roomDelete(Request $request){
        $info = GameRoom::where('id',$request->param('id'))->find();
        if($info['status'] != 0){
            return json(['code'=>1,'message'=>'房间正在运行']);
        }
        if(GameRoom::where('id',$request->param('id'))->delete()){
            return json(['code'=>0,'message'=>'删除成功']);
        }
        return json(['code'=>1,'message'=>'删除失败']);
    }




    public function config(){
        $config = GameRoomConfig::select();
        return $this->render('config',[
            'config'=>$config,
        ]);
    }


    public function configSave(Request $request){
        $id = $request->post('id');


        $log = array(
            'name' => $request->post('name'),
            'size' => $request->post('size'),
            'money' => $request->post('money'),
            'percentage' => $request->post('percentage'),

        );
        $res = GameRoomConfig::where('id', $id)->update($log);
        if (!$res) {
            return ['code' => 1, 'message' => '修改失败'];
        }
        return ['code' => 0, 'message' => '修改成功'];
    }

    public function room_log(Request $request){
        $config = GameRoomConfig::select();
        $entity = GameRoomLog::alias('l')->field('l.*,r.name as room_name')
            ->join('game_room r','r.id = l.room_id');

        if($type = $request->param('type')){
            $entity->where('r.size',$type);
        }
        $list = $entity
            ->order('id desc')
            ->paginate(15, false, [
                'query' => $request->param()??[]
            ]);
        return $this->render('room_log',[
            'list'=>$list,
            'cate'=>$config,
        ]);
    }

}